package sii.fb.emotion.fetch;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;

import java.util.Map;
import java.util.StringTokenizer;

import sii.fb.emotion.bean.Post;
import sii.fb.emotion.bean.User;


public class DataFetch {
	
	public Map<String, List<Integer>> word2categories(String wordsFile) {
		Map<String, List<Integer>> word2cat = new HashMap<String, List<Integer>>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(wordsFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String line = null;
		try {
			while((line=reader.readLine())!=null){
				StringTokenizer s = new StringTokenizer(line);
				List<Integer> categories = new LinkedList<Integer> ();
				String word = s.nextToken();
				while(s.hasMoreTokens()){
					Integer cat = Integer.parseInt(s.nextToken());
					categories.add(cat);
				}
				if(!word2cat.containsKey(word)){
					word2cat.put(word, categories);
				}
				else{
					List<Integer> l = word2cat.get(word);
					for(Integer j: categories){
						if(!l.contains(j)){
							l.add(j);
						}
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return word2cat;
	}

	public List<String> fetchCategories(String categoriesFile) {
		List<String> categories = new LinkedList<String>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(categoriesFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String line = null;
		try {
			while((line=reader.readLine())!=null){
				String cat = line.substring(line.indexOf(" ")+1, line.length());
				categories.add(cat);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return categories;
	}
	


	public Map<Long, User> createUserList(String usersFile){
		Map<Long, User> mapUsers = new HashMap<Long,User>();
		User u;
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(usersFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String line = null;
		try {
			line = reader.readLine();
			while((line=reader.readLine())!=null){
				u = new User();
				StringTokenizer s = new StringTokenizer(line, ";");
				int i = 0;
				while(s.hasMoreTokens()){
					String token = s.nextToken();
					switch(i){
					case 0: u.setId(Long.parseLong(token));break;
					case 1: u.setName(token);break;
					case 2: u.setBirtday(token);break;
					case 3: u.setGender(token);break;
					case 4:u.setHometown(token);break;
					}
					i++;
				}
				if(!mapUsers.containsKey(u.getId()))
					mapUsers.put(u.getId(), u);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return mapUsers;
	}
	
	public Map<Long, List<Post>> createUser2Posts(String postsFile){
		Map<Long, List<Post>> user2posts = new HashMap<Long,List<Post>>();
		Post p;
		Long idUser = null;
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(postsFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String line = null;
			try {
				line=reader.readLine();
				while(line!=null){
					while(line.startsWith("user=")){
						idUser = Long.parseLong(line.substring(5, line.indexOf(";")));
						line = reader.readLine();
						if(!user2posts.containsKey(idUser)){
							List<Post> posts = new LinkedList<Post>();
							user2posts.put(idUser, posts);
						}
					}
					if(!line.endsWith(";")){
						do{
							line +=reader.readLine();
						}while(!line.endsWith(";"));
					}
					p = new Post();
					StringTokenizer s = new StringTokenizer(line, ";");
					int i = 0;
					while(s.hasMoreTokens()){
						String token = s.nextToken();
						switch(i){
						case 0: p.setId(Long.parseLong(token));break;
						case 1: p.setMessage(token);break;
						case 2: p.setUpdate(token);break;
						}
						i++;
					}
					user2posts.get(idUser).add(p);
					line = reader.readLine();
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		return user2posts;
	}
		
}
